/*
/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Game packet.
 *
 * Game packet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Game packet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game packet.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.sound;

import java.util.ArrayList;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Transmitter;


public class MidiInput implements Receiver {

	protected MidiPlayer midiPlayer;
	protected ArrayList<MidiDevice> deviceList = new ArrayList<MidiDevice>();
	
	public void init(MidiPlayer player) {
		this.midiPlayer = player;
		
		MidiDevice.Info[]	aInfos = MidiSystem.getMidiDeviceInfo();
		
		// läytyikä?
		if (aInfos.length == 0)
		{
			System.out.println("no device infos found.");
			return;
		}	

		// Load input devices and set transmitters..
		for(int i=0; i<aInfos.length; i++) {
			System.out.print("Device name: " +aInfos[i].getName());
			try
			{
				MidiDevice device = MidiSystem.getMidiDevice(aInfos[i]);
				boolean bAllowsInput = (device.getMaxTransmitters() != 0);
				if(device.getMaxReceivers() != 0) {
					System.out.print(" OUT" );
				}
				if(bAllowsInput) {
					device.open();
					deviceList.add(device);
					System.out.print(" IN, using this" );
					Transmitter	t = device.getTransmitter();
					t.setReceiver(this);
				}
				System.out.println(".");
			}
			catch (MidiUnavailableException e)
			{
					e.printStackTrace();
			}	
		}	
		
	}

	@Override
	public void close() {
		while(!deviceList.isEmpty()) {
			deviceList.get(0).close();
			deviceList.remove(0);
		}
	}

	@Override
	public void send(MidiMessage message, long lTimeStamp)
	{
		if (message instanceof ShortMessage)
		{
			handleMessage((ShortMessage) message);
		}
	}

	// Tämä sitten käsittelee saadut viestit..
	public String handleMessage(ShortMessage message)
	{
		String	strMessage = null;
		switch (message.getCommand())
		{
		// Note off..
		case 0x80:
			strMessage = "note Off " + MidiPlayer.getKeyName(message.getData1()) + " velocity: " + message.getData2();
			midiPlayer.playEkeys(message.getData1(), 0);
			break;

	    // note on..
		case 0x90:
			
			midiPlayer.playEkeys(message.getData1(), message.getData2());
			strMessage = "note On " + MidiPlayer.getKeyName(message.getData1()) + " velocity: " + message.getData2();
			break;


		default:
			strMessage = "unknown message: status = " + message.getStatus() + ", byte1 = " + message.getData1() + ", byte2 = " + message.getData2();
			break;
		}
		
		// Ei mitään aavistusta mitä allaoleva tekee..
		if (message.getCommand() != 0xF0)
		{
			int	nChannel = message.getChannel() + 1;
			String	strChannel = "channel " + nChannel + ": ";
			strMessage = strChannel + strMessage;
		}
		return strMessage;
	}

	@Override
	protected void finalize() throws Throwable {
		while(!deviceList.isEmpty()) {
			deviceList.get(0).close();
			deviceList.remove(0);
		}
		super.finalize();
	}

	public long getDevicesCount() {
		return deviceList.size();
	}
}
